package com.xinqi.modules.sys.textbook.manager.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.enums.YesNoEnum;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.textbook.domain.CloudTextbookContentEntity;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookContentQueryDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookContentResultDTO;
import com.xinqi.modules.sys.textbook.manager.CloudTextbookContentManager;
import com.xinqi.modules.sys.textbook.mapper.CloudTextbookContentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 云课本课堂内容管理层实现
 *
 * @author: linkai
 * @date: 2023/04/25
 */
@Service
@RequiredArgsConstructor
public class CloudTextbookContentManagerImpl extends BaseServiceImpl<CloudTextbookContentMapper, CloudTextbookContentEntity> implements CloudTextbookContentManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<CloudTextbookContentResultDTO> findPage(Page<Object> page, CloudTextbookContentQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<CloudTextbookContentResultDTO> findList(CloudTextbookContentQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 云课本课堂内容数据
     */
    @Override
    public CloudTextbookContentEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 云课本课堂内容数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public void cleanByCatalogId(Long catalogId) {
        LambdaUpdateWrapper<CloudTextbookContentEntity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(CloudTextbookContentEntity::getRefId, catalogId);
        wrapper.set(CloudTextbookContentEntity::getRefId, null);
        wrapper.set(CloudTextbookContentEntity::getIsRef, YesNoEnum.NO.ordinal());
        update(wrapper);
    }

}
